﻿'
' Quiz Module for DotNetNuke
' Project Contributors - Will Strohl (http://www.WillStrohl.com)
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Quiz Module for DotNetNuke, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.UI.Skins.Skin
Imports DotNetNuke.UI.Skins.Controls.ModuleMessage
Imports DotNetNuke.Services.Exceptions
Imports WillStrohl.Modules.Quiz.QuizInfoMembers

Namespace WillStrohl.Modules.Quiz

    Partial Public MustInherit Class ViewQuiz
        Inherits WnsPortalModuleBase
        Implements IActionable

#Region " Private Members "

        Private Const EDIT_KEY As String = "Edit"
        Private Const LIST_KEY As String = "List"
        Private Const MENU_ITEM_EDIT_KEY As String = "EditQuiz.MenuItem.Title"
        Private Const MENU_ITEM_LIST_KEY As String = "QuizList.MenuItem.Title"

        Private p_QuizCount As Integer = Null.NullInteger
        Private p_QuizId As Integer = Null.NullInteger
        Private p_Quiz As QuizInfo = Nothing
        Private p_QuizName As String = Null.NullString
        Private p_QuizDescription As String = Null.NullString
        Private p_QuizLinkUrl As String = Null.NullString
        Private p_Quizzes As QuizInfoCollection = Nothing

#End Region

#Region " Private Properties "

        Private ReadOnly Property QuizCount As Integer
            Get
                If Not p_QuizCount > Null.NullInteger Then
                    ' the SProc doesn't work right now
                    'Dim ctlQuiz As New QuizController
                    'p_QuizCount = ctlQuiz.GetQuizCount(PortalId)
                    p_QuizCount = Quizzes.Count()
                End If

                Return p_QuizCount
            End Get
        End Property

        Private ReadOnly Property QuizId As Integer
            Get
                If p_QuizId = Null.NullInteger Then
                    Dim obj As Object = Settings(QUIZ_ID_FIELD)
                    If Not obj Is Nothing AndAlso IsNumeric(obj) Then
                        p_QuizId = Integer.Parse(obj.ToString(), Globalization.NumberStyles.Integer)
                    End If
                End If

                Return p_QuizId
            End Get
        End Property

        Private ReadOnly Property Quiz As QuizInfo
            Get
                If p_Quiz Is Nothing Then
                    Dim ctlQuiz As New QuizController
                    p_Quiz = ctlQuiz.GetQuiz(QuizId)
                End If

                Return p_Quiz
            End Get
        End Property

        Protected ReadOnly Property QuizName As String
            Get
                If String.IsNullOrEmpty(p_QuizName) And Not Quiz Is Nothing Then
                    p_QuizName = Quiz.QuizName
                End If

                Return p_QuizName
            End Get
        End Property

        Protected ReadOnly Property QuizDescription As String
            Get
                If String.IsNullOrEmpty(p_QuizDescription) And Not Quiz Is Nothing Then
                    p_QuizDescription = Quiz.QuizDescription
                End If

                Return p_QuizDescription
            End Get
        End Property

        Protected ReadOnly Property QuizLinkUrl As String
            Get
                If String.IsNullOrEmpty(p_QuizLinkUrl) Then
                    p_QuizLinkUrl = NavigateURL(TabId, PortalSettings, "Quiz", String.Format("qi={0}", QuizId))
                End If

                Return p_QuizLinkUrl
            End Get
        End Property

        Private ReadOnly Property Quizzes As QuizInfoCollection
            Get
                If p_Quizzes Is Nothing Then
                    Dim ctlQuiz As New QuizController
                    p_Quizzes = ctlQuiz.GetQuizzes(PortalId)
                End If

                Return p_Quizzes
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try

                If Not Me.Page.IsPostBack Then
                    Me.BindData()
                End If

            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

#End Region

#Region " Helper Methods "

        Private Sub BindData()

            Me.LocalizeModule()

            pnlQuiz.Visible = (QuizCount > 0 AndAlso QuizId > Null.NullInteger)
            If QuizCount = 0 Then
                AddModuleMessage(Me, GetLocalizedString("Error.NoQuizzes"), ModuleMessageType.RedError)
            End If
            If QuizId = Null.NullInteger Then
                AddModuleMessage(Me, GetLocalizedString("Error.NoQuizAttached"), ModuleMessageType.RedError)
            End If

        End Sub

        Private Sub LocalizeModule()



        End Sub

#End Region

#Region " IActionable Implementation "

        Public ReadOnly Property ModuleActions() As Actions.ModuleActionCollection Implements IActionable.ModuleActions
            Get
                Dim oActions As New Actions.ModuleActionCollection

                oActions.Add(GetNextActionID, Me.GetLocalizedString(MENU_ITEM_EDIT_KEY), _
                    String.Empty, String.Empty, String.Empty, _
                    EditUrl(String.Empty, String.Empty, EDIT_KEY), _
                    False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

                If QuizCount > 0 Then
                    oActions.Add(GetNextActionID, Me.GetLocalizedString(MENU_ITEM_LIST_KEY), _
                        String.Empty, String.Empty, String.Empty, _
                        EditUrl(String.Empty, String.Empty, LIST_KEY), _
                        False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                End If

                Return oActions
            End Get
        End Property

#End Region

        Private Sub cmdDeleteSettings_Click(sender As Object, e As System.EventArgs) Handles cmdDeleteSettings.Click
            Dim ctlModule As New ModuleController
            ctlModule.DeleteModuleSettings(ModuleId)
            ctlModule.DeleteTabModuleSettings(TabModuleId)
            ModuleController.SynchronizeModule(ModuleId)
            Response.Redirect(NavigateURL())
        End Sub
    End Class

End Namespace